package com.imooc.ecommerce.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * 商品状态枚举类
 *
 * @author zhangshao
 * @since 2023/11/29 21:29
 */
@Getter
@AllArgsConstructor
public enum GoodsStatus {
    ONLINE(101, "上线"),
    OFF_LINE(102, "下线"),
    STOCK_OUT(103, "缺货");
    /**
     * 状态码
     */
    @EnumValue
    private final Integer status;

    /**
     * 状态描述
     */
    @JsonValue
    private final String description;

    public static GoodsStatus of(Integer status) {
        Objects.requireNonNull(status);
        return Stream.of(values())
                .filter(bean -> bean.status.equals(status))
                .findAny()
                .orElseThrow(
                () -> new IllegalArgumentException(status + " not exists")
        );

    }
}
